import { useQuery } from "@apollo/react-hooks";
import gql from "graphql-tag";

export const SYS_AUTHORITIES = gql`
  query SysAuthorities {
    sysAuthorities {
      id
      description
      children {
        id
        description
        children {
          id
          description
        }
      }
    }
  }
`;

export const useAuthorities = () => {
  const { data } = useQuery(SYS_AUTHORITIES);
  const authorities =
    (data &&
      data.sysAuthorities &&
      data.sysAuthorities.map(({ id, description, children }) => ({
        title: description,
        key: id,
        value: id,
        children:
          children &&
          children.map(({ id, description, children }) => ({
            title: description,
            key: id,
            value: id,
            children:
              children &&
              children.map(({ id, description }) => ({
                title: description,
                key: id,
                value: id
              }))
          }))
      }))) ||
    [];
  return { authorities };
};
